"""
视频号面板类，作为主窗口中的顶级选项卡
"""
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import pyqtSignal

from app.ui.modules.channel_module import ChannelModule
from app.utils.logger import Logger
from app.services.service_locator import ServiceLocator
from app.services.config_service import IConfigService

class ChannelPanel(QWidget):
    """
    视频号面板类，作为主窗口的顶级选项卡
    """
    
    # 定义信号
    config_updated = pyqtSignal(str, object)  # 配置项名称, 值
    
    def __init__(self, parent=None):
        """
        初始化视频号面板
        """
        super().__init__(parent)
        
        # 获取日志记录器
        self.logger = Logger.get_logger("channel_panel")
        
        # 初始化UI
        self._init_ui()
        
        # 加载配置
        self.load_config()
        
        self.logger.info("视频号面板初始化完成")
    
    def _init_ui(self):
        """
        初始化UI
        """
        # 创建主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(10, 10, 10, 10)
        main_layout.setSpacing(10)
        
        # 创建视频号模块
        self.channel_module = ChannelModule()
        self.channel_module.channel_config_updated.connect(self._on_channel_config_updated)
        
        # 添加到主布局
        main_layout.addWidget(self.channel_module)
    
    def _on_channel_config_updated(self, key, value):
        """
        处理视频号配置更新事件
        
        @param key: 配置键
        @param value: 配置值
        """
        self.logger.info(f"视频号配置已更新: {key}={value}")
        
        # 转发配置更新信号
        self.config_updated.emit(key, value)
        
        # 获取配置服务
        config_service = ServiceLocator.get(IConfigService)
        if config_service:
            # 更新配置
            config_service.set(key, value)
            
            # 保存配置
            config_service.save_config()
            self.logger.info(f"视频号配置已保存: {key}={value}")
        else:
            self.logger.warning("无法获取配置服务，视频号配置未保存")
            
    def load_config(self):
        """
        加载配置
        """
        try:
            # 获取配置服务
            config_service = ServiceLocator.get(IConfigService)
            if not config_service:
                self.logger.warning("无法获取配置服务，无法加载视频号配置")
                return
                
            # 获取配置
            config = config_service.get_all()
            if not config:
                self.logger.warning("无法获取配置")
                return
                
            # 加载置顶话术相关配置
            pinned_enabled = config.get("pinned", {}).get("enabled", False)
            pinned_keyword = config.get("pinned", {}).get("keyword", "")
            pinned_frequency = config.get("pinned", {}).get("frequency", 5)
            
            # 加载直播状态相关配置
            live_status = config.get("live_status", "未知")
            viewers = config.get("viewers", 0)
            
            # 记录加载的配置
            self.logger.info(f"加载视频号配置: 启用置顶={pinned_enabled}, 关键词={pinned_keyword}, 频率={pinned_frequency}分钟, 直播状态={live_status}, 观众={viewers}")
            
            # 将配置设置到视频号模块
            self.channel_module.set_config(
                pinned_enabled=pinned_enabled, 
                pinned_keyword=pinned_keyword, 
                pinned_frequency=pinned_frequency,
                live_status=live_status,
                viewers=viewers
            )
            
        except Exception as e:
            self.logger.error(f"加载视频号配置失败: {str(e)}")
            import traceback
            self.logger.error(traceback.format_exc()) 